<template>
	<view>
		<view class="head_box">
			<view class="filter-item">
				<sh-filter @onConfirm="hanleSort" :dicData="dicData" @onSearch="handleSearch" :perData="personData"
					:sortItems="sortItems" @onRanking='handleRanking' :sortList='sortList'></sh-filter>
			</view>
		</view>
		<list-item :fields="fields" :dataSource="listData" @onClick="toDetail" :showCheckBox="showChk" ref="listchild"
			@onSelect="getInSelected"></list-item>
		<mix-float-menu @onBtnClick="clickFloatBtn" @onClose="close" @add="addNewPage" :isShow="showItem" ref="child"
			@onDelete='handleDelete' @onExport='handleExport'>
		</mix-float-menu>
		<mix-export :selectIds="selectIds" :queryString="queryString" @onClose="closeExport" :show="showExport"
			:type="exportType"></mix-export>
	</view>
</template>

<script>
	import ListItem from '../../../components/mix-list-cell/mix-list-cell.vue';
	import mixSortBar from '../../../components/mix-sort-bar.vue';
	import mixFloatMenu from '../../../components/mix-float-menu.vue';
	import shFilter from '../../pulic/sh-filter.vue';
	import mixExport from '../../../components/mix-export.vue';
	export default {
		components: {
			ListItem,
			mixSortBar,
			mixFloatMenu,
			shFilter,
			mixExport
		},
		data() {
			return {
				showChk: false,
				code: '',
				name: '',
				listData: [],
				pageNumber: 1,
				fields: {},
				total: 0,
				moreTip: false,
				showItem: 'new,delete,export',
				selectedIndex: [],
				formData: {
					searchText: ''
				},
				dicData: {

				},
				personData: [],
				sortItems: '',
				sortList: [],
				queryString: '',
				selectIds: '',
				showExport: false,
				exportType: 'qy_excel'
			}
		},
		methods: {
			hanleSort(e) {

				const formsData = {
					...this.formData,
					...e
				};
				this.pageNumber = 1;
				formsData.searchText = '';
				this.formData = formsData;
				this.getList(this.code)
			},
			handleSearch(e) {
				console.log('e', e)
				this.formData = {}
				this.formData.searchText = e;
				this.getList(this.code)
			},
			handleRanking(data) {

				this.sortList.forEach((item, index) => {
					if (data !== index) {
						item.sortOrder = ''
					}
				})
				if (this.sortList[data].sortOrder) {
					if (this.sortList[data].sortOrder === 'asc') {
						this.sortList[data].sortOrder = 'desc'
					} else {
						this.sortList[data].sortOrder = 'asc'
					}
				} else {
					this.sortList[data].sortOrder = 'asc'
				}
				this.formData.sortField = this.sortList[data].sortField;
				this.formData.sortOrder = this.sortList[data].sortOrder;
				this.getList(this.code)
			},
			toDetail(index) {

				const dataKey = [...this.listData][index].DATAKEY;
				uni.navigateTo({
					url: '../detail/detail?code=' + this.code + '&name=' + this.name + '&dataKey=' + dataKey
				})
			},
			clickFloatBtn() {
				this.showChk = true
			},
			close() {
				this.showChk = false;
				this.$refs.listchild.reset();
			},
			addNewPage() {
				const path = `../edit/${this.code}?code=${this.code}`
				uni.navigateTo({
					url: path
				})
			},
			//删除
			async handleDelete() {
				//info/busEnterprise/remove.do
				const self = this;
				console.log(this.selectedIndex)
				if (!this.selectedIndex.length) {
					uni.showToast({
						title: '请至少选择一项',
						icon: 'none'
					})
					return;
				}
				uni.showModal({
					title: '确定删除吗?',
					async success(e) {
						uni.showLoading({
							title: '删除中...'
						})
						if (e.confirm) {
							const arr = [];
							self.selectedIndex.forEach((item, index) => {
								arr.push(self.listData[item].DATAKEY)
							})
							const res = await self.$req({
								url: `info/${self.code}/remove.do`,
								data: {
									selectedIds: arr.join(',')
								}
							})
							uni.hideLoading()
							if (res.data.success) {
								console.log('in in in')
								uni.showToast({
									title: res.data.messages[0].content,
									icon: 'success',
									duration: 2000
								})
								self.$refs.child.handleClose()
								self.showChk = false;
								self.getList(self.code);
							} else {
								uni.showToast({
									title: res.data.messages[0].content,
									icon: 'none'
								})
							}
						}
					}
				})

			},
			closeExport() {
				this.showExport = false
			},
			handleExport() {
				this.showExport = true
				const arr = [];
				this.selectedIndex.forEach((item, index) => {
					arr.push(this.listData[item].DATAKEY)
				})
				this.selectIds = arr.join(',')
			},
			getInSelected(e) {
				console.log(e)
				this.selectedIndex = e;
			},
			getListFields(code) {
				switch (code) {
					case 'busEnterprise':
						this.fields = {
							name: "NAME",
							date: "CREATE_TIME",
							person: "MANAGER",
							columns: [{
								title: '经营范围',
								dataKey: 'COMPANY_PROFILE'
							}, {
								title: '行业类别',
								dataKey: 'industry'
							}, {
								title: '企业规模',
								dataKey: 'COMPANY_SIZE'
							}]
						}
						this.sortItems = "industry,companySize,createTime,person,city";
						this.sortList = [{
								sortField: 'CREATE_TIME', //字段名称
								sortOrder: '', //排序方式
								title: '建档日期', //字段名称
							},
							{
								sortField: 'MANAGER', //字段名称
								sortOrder: '', //排序方式
								title: '跟进人', //字段名称
							},
							{
								sortField: 'DIS', //字段名称
								sortOrder: '', //排序方式
								title: '地区', //字段名称
							},
							{
								sortField: 'DIS', //字段名称
								sortOrder: '', //排序方式
								title: '地区', //字段名称
							},
						]
						this.exportType = "qy_excel"
						break;
					case 'busInvestor':
						this.fields = {
							name: "NAME",
							date: "CREATE_TIME",
							person: "MANAGER",
							columns: [{
									title: '企业简介',
									dataKey: 'COMPANY_PROFILE'
								},
								{
									title: '投资商类型',
									dataKey: 'TYPE'
								},
								{
									title: '投资领域',
									dataKey: 'INVESTORS_INDUSTRY'
								},
								{
									title: '企业规模',
									dataKey: 'COMPANY_SIZE'
								}
							],

						}
						this.sortItems = "investFiled,investType,createTime,person";
						this.sortList = [{
								sortField: 'CREATE_TIME', //字段名称
								sortOrder: '', //排序方式
								title: '建档日期', //字段名称
							},
							{
								sortField: 'COMPANY_SIZE', //字段名称
								sortOrder: '', //排序方式
								title: '企业规模', //字段名称
							},
							{
								sortField: 'MANAGER', //字段名称
								sortOrder: '', //排序方式
								title: '跟进人', //字段名称
							},
						]
						this.exportType = "tzs_excel"
						break;
					case 'busOrg':
						this.fields = {
							name: "NAME",
							date: "CREATE_TIME",
							person: "MANAGER",
							columns: [{
									title: '机构简介',
									dataKey: 'COMPANY_PROFILE'
								}, {
									title: '机构类型',
									dataKey: 'TYPE'
								},
								{
									title: '会员企业数',
									dataKey: 'MEMBER_COUNT'
								}
							]
						}
						this.sortItems = "orgType,createTime,person";
						this.sortList = [{
								sortField: 'CREATE_TIME', //字段名称
								sortOrder: '', //排序方式
								title: '建档日期', //字段名称
							},
							{
								sortField: 'MEMBER_COUNT', //字段名称
								sortOrder: '', //排序方式
								title: '会员企业数', //字段名称
							},
							{
								sortField: 'MANAGER', //字段名称
								sortOrder: '', //排序方式
								title: '跟进人', //字段名称
							},
						]
						this.exportType = "jg_excel"
						break;
					case 'busExhibition':
						this.fields = {
							name: "NAME",
							date: "START_DATE",
							person: "MANAGER",
							columns: [{
								title: '行业类别',
								dataKey: 'extIndustry'
							}, {
								title: '主办单位',
								dataKey: 'ORGANIZERS'
							}]
						}
						this.sortItems = "industry,createTime,person";
						this.sortList = [{
								sortField: 'START_DATE', //字段名称
								sortOrder: '', //排序方式
								title: '日期', //字段名称
							},
							{
								sortField: 'PLACE', //字段名称
								sortOrder: '', //排序方式
								title: '地点', //字段名称
							},
							{
								sortField: 'MANAGER', //字段名称
								sortOrder: '', //排序方式
								title: '跟进人', //字段名称
							},
						]
						this.exportType = "zh_excel"
						break;
					case 'busTarget':
						this.fields = {
							name: "NAME",
							date: "CREATE_TIME",
							person: "MANAGER",
							columns: [{
									title: '企业简介',
									dataKey: 'COMPANY_PROFILE'
								}, {
									title: '经营范围',
									dataKey: 'BUSINESS_SCOPE'
								},
								{
									title: '行业类别',
									dataKey: 'industry'
								}
							]
						}
						this.sortItems = "industry,companySize,companyType,createTime,person";
						this.sortList = [{
								sortField: 'CREATE_TIME', //字段名称
								sortOrder: '', //排序方式
								title: '建档日期', //字段名称
							},
							{
								sortField: 'COMPANY_SIZE', //字段名称
								sortOrder: '', //排序方式
								title: '企业规模', //字段名称
							},
							{
								sortField: 'MANAGER', //字段名称
								sortOrder: '', //排序方式
								title: '跟进人', //字段名称
							},
						]
						this.exportType = "mbqy_excel"
						break
					default:
						break;
				}
			},

			async getList(moduleId) {

				const self = this;
				uni.showLoading({

				})
				const dicData = JSON.parse(uni.getStorageSync('dicData'));
				this.dicData = dicData;
				const ret = await this.$req({
					url: `info/${this.code}/filter.do`,
					data: {
						moduleId: moduleId
					}
				})
				let personData = []
				if (ret.data.success) {
					personData = ret.data.attributes.zdyDataSource.personNameDataSourceManage ?
						ret.data.attributes.zdyDataSource.personNameDataSourceManage : [];
					const personList = ret.data.attributes.zdyDataSource.personDataSourceManage ?
						ret.data.attributes.zdyDataSource.personDataSourceManage : [];
					this.personData = personData;
				}
				const basicParams = {
					pageSize: "10", //每页条数
					pageNo: this.pageNumber, //页码
					moduleId: moduleId //固定值
				}

				const params = {
					...this.formData,
					...basicParams
				}
				if (params.MANAGER == '') {
					delete params.MANAGER
				}
				const res = await this.$req({
					url: `info/${this.code}/ajaxSearch.do`,
					data: params,
				})
				uni.hideLoading()
				const list = this.listData;
				const rows = [...res.data.attributes.rows]
				this.queryString = res.data.attributes.queryString;

				rows.forEach(item => {
					item.industry = item.INDUSTRY && this.$util.queryDicName(dicData, 'inve_field', item
						.INDUSTRY)
					item.CREATE_TIME = item.CREATE_TIME && item.CREATE_TIME.split(' ')[0]
					//企业规模e nt_scale
					item.COMPANY_SIZE = this.$util.queryDicName(dicData, 'ent_scale', item.COMPANY_SIZE)
					//投资领域 INVESTORS_INDUSTRY
					item.INVESTORS_INDUSTRY = item.INVESTORS_INDUSTRY && this.$util.queryDicName(dicData,
						'inve_field', item.INVESTORS_INDUSTRY)
					//投资商类型
					item.TYPE = item.TYPE && this.$util.queryDicName(dicData, 'inve_type', item.TYPE)
					//跟进人
					item.MANAGER = this.$util.queryPersonName(item.MANAGER, personData)
					//展会的行业类型
					if (this.code === 'busExhibition') {
						item.extIndustry = this.$util.queryDicNameMulti(dicData, 'inve_field', item.INDUSTRY)

					}

				})

				let newArr = [];
				newArr = this.pageNumber <= 1 ? rows : list.concat(rows)
				this.listData = newArr;
				this.total = res.data.attributes.total

			},

		},

		onLoad(options) {
			this.code = options.code;
			this.name = options.name;
			this.getListFields(options.code)
			this.getList(options.code)

			uni.setNavigationBarTitle({
				title: options.name
			})

		},
		onShow() {
			this.showChk = false;
			this.showSideMenu = false;
		},
		onReachBottom() {
			const totalPage = Math.ceil(this.total / 10);
			const currPage = this.pageNumber;
			if (currPage < totalPage) {
				this.pageNumber++;
				this.getList()
			} else {
				this.moreTip = true
			}
		},
		onShareAppMessage() {
			return {
				imageUrl: 'https://www.szaez.com/sck/share.jpg?v=' + new Date().getTime(),
				path: '/pages/setionManerge/list/list?code=' + this.code + '&name=' + this.name
			}
		}
	}
</script>

<style>

</style>
